package zack.project.domain.activity.service.quota.rule.impl;

import zack.project.domain.activity.model.entity.ActivityCountEntity;
import zack.project.domain.activity.model.entity.ActivityEntity;
import zack.project.domain.activity.model.entity.ActivitySkuEntity;
import zack.project.domain.activity.model.valobj.ActivityStateVO;
import zack.project.domain.activity.service.quota.rule.AbstractActionChain;
import zack.project.types.enums.ResponseCode;
import zack.project.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author A1793
 * 活动基础信息过滤
 */
@Slf4j
@Component("activity_base_action")
public class ActivityBaseActionChain extends AbstractActionChain {
    @Override
    public boolean action(ActivitySkuEntity activitySkuEntity,
                          ActivityEntity activityEntity,
                          ActivityCountEntity activityCountEntity) {

        log.info("活动责任链-基础信息【有效期、状态】校验开始。");
        //检验抽奖活动是否开启
        if(!ActivityStateVO.open.equals(activityEntity.getState())) {
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        Date curTime = new Date();
        //看当前时间是否可以参加抽奖活动
        if(activityEntity.getBeginDateTime().after(curTime)||activityEntity.getEndDateTime().before(curTime)) {
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        Integer stockCountSurplus = activitySkuEntity.getStockCountSurplus();
        if(stockCountSurplus <= 0) {
            throw new AppException(ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getCode(), ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getInfo());
        }

        return next().action(activitySkuEntity, activityEntity, activityCountEntity);
    }
}
